﻿//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//

#include "pch.h"
#include "MainPage.xaml.h"

#include <ppltasks.h>
using namespace concurrency;

using namespace TCPcli2;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Popups;
using namespace Windows::Networking;
using namespace Windows::Networking::Sockets;
using namespace Windows::Storage::Streams;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

MainPage::MainPage()
{
	InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.  The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
	(void) e;	// Unused parameter
}

void MainPage::CloseSocket(){
	socket->Close();
}


void TCPcli2::MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	//Los de hostname op
	HostName^ hostName;
    try{
        hostName = ref new HostName(txtbxIP->Text);
    }
    catch (InvalidArgumentException^ e){
		auto msgBox = ref new MessageDialog("Er was een probleem met het IP-adres: " + e->Message,"Error IP-adres");
		msgBox->ShowAsync();
		//return;
	}

	socket = ref new StreamSocket();

	try{
		socket->ConnectAsync(hostName, txtbxPort->Text);
	}
	catch(Exception^ e){
		auto msgBox = ref new MessageDialog("Er was een probleem met verbinden: " + e->Message,"Error Socket connect");
        msgBox->ShowAsync();
		//return;
	}
	
	
	txtblckConnection->Text = "Verbinding gemaakt";

	outputStream = ref new DataWriter(socket->OutputStream);
	inputStream = ref new DataReader(socket->InputStream);
	//grdSend->Visibility = Windows::UI::Xaml::Visibility::Visible;
}


void TCPcli2::MainPage::btnSend_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	unsigned int intStrLen = outputStream->MeasureString(txtbxSend->Text);

	try{
		//outputStream->WriteInt32(intStrLen);
		outputStream->WriteString(txtbxSend->Text);
		outputStream->StoreAsync();
		//outputStream->DetachStream();
		//delete[] outputStream;
	}
	catch(Exception^ e){
		auto msgBox = ref new MessageDialog("Probleem met versturen: " + e->Message,"Error data write");
		msgBox->ShowAsync();
		return;
	}
	
	
	//inputStream->InputStreamOptions = InputStreamOptions::Partial;
	create_task(inputStream->LoadAsync(intStrLen)).then([this](unsigned int numBytesLoaded){
		String^ message = "empty";
		try{	
			message = inputStream->ReadString(numBytesLoaded);
			//inputStream->DetachStream();
			//delete[] inputStream;
			//unsigned int stringLen = inputStream->ReadInt32();
			//auto inputStuff2 = inputStream->LoadAsync(inputLen);
		}
		catch(Exception^ e){
			auto msgBox = ref new MessageDialog("Probleem met ontvangen: " + e->Message,"Error data read");
			msgBox->ShowAsync();
		}
		txtblckReceived->Text = numBytesLoaded + " " + message;
	});

	//auto msgBox = ref new MessageDialog("Bericht succesvol verstuurd","Versturen succesvol");
	//msgBox->ShowAsync();
}